<template>
	<section>
		<!--工具条 高级查询 新增 -->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表  编辑 修改-->
		<el-table :data="pageIfon.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="员工" width="100" sortable>
			</el-table-column>
			<el-table-column prop="password" label="密码" width="170"  sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="150" sortable>
			</el-table-column>
			<el-table-column prop="headImage" label="头像" width="140" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" min-width="80" sortable>
			</el-table-column>
      <el-table-column prop="department.name" label="部门" min-width="100" sortable>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条  批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :current-page="query.currentPage"
                     :page-size="query.pageSize"
                     :total="pageIfon.total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--新增界面-->
		<el-dialog title="新增/修改" :visible.sync="employeeFormVisible" :close-on-click-modal="false">
			<el-form :model="employeeForm" label-width="80px" :rules="employeeFormRules" ref="employeeForm">
				<el-form-item label="员工" prop="username">
					<el-input v-model="employeeForm.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="密码">
          <el-input v-model="employeeForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="employeeForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="employeeForm.headImage" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="employeeForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门">
<!--          <el-input v-model="employeeForm.parent.id" auto-complete="off"></el-input>-->
          <el-cascader v-model="employeeForm.department.name"
                       :options="deptTree"
                       :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
          clearable></el-cascader>
        </el-form-item>
      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="employeeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="employeeFormLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
			  query:{
			    pageSize:10,
          currentPage:1,
          keyword:''
        },
        pageIfon:{
			    rows:[],
          total:0
        },
				listLoading: false,
				sels: [],//列表选中列
        
				employeeFormVisible: false,//新增界面是否显示
				employeeFormLoading: false,
				employeeFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
        employees:[],
        deptTree:[],
				//新增修改界面数据
				employeeForm: {
			    id:null,
					username:"",
          password:"",
          email:"",
          headImage:"",
          age:"",
          department:{
			      id:"",
            name:""
          }
				}
			}
		},
		methods: {
      search(val){
        this.query.currentPage = 1;
        this.getEmployees()
      },
      handleCurrentChange(val){
        this.query.currentPage = val;
        this.getEmployees()
      },
			//获取员工列表
			getEmployees() {
				this.listLoading = true;
				this.$http.post("/employee",this.query)
                        .then(result =>{
                          result = result.data;
                          if(result.success){
                            this.pageIfon = result.result;
                            this.listLoading = false;
                          }else {
                            this.$message({
                              message: '分页查询失败！'+result.message,
                              type: 'error'
                            });
                          }
                        })
                        .catch(result =>{
                          this.$message({
                            message: '分页查询失败！',
                            type: 'error'
                          });
                        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/employee/"+row.id)
                    .then(result =>{
                      result = result.data;
                      if(result.success){
                        this.$message({message: '删除成功', type: 'success'});
                        this.query.currentPage = 1;
                        this.getEmployees()
                      }else{this.$message({message: '删除失败'+result.message, type: 'error'});}
                    })
				}).catch(() => {
          this.$message({message: '已取消删除', type: 'info'});
				});
			},
      gteEmployeeList(){
        this.$http.get("/employee")
                  .then(result =>{
                    result = result.data;
                    if(result.success){
                      this.employees = result.result;
                    }else {
                      this.$message({message: '获取员工数据失败' + result.message, type: 'error'})
                    }
                  }).catch(result =>{
                    this.$message({message: '获取员工数据失败' + result.message, type: 'error'})
               })
      },
			//显示编辑界面
			handleEdit: function (index, row) {
				this.employeeFormVisible = true;
				this.employeeForm = Object.assign({}, row);
				if(this.employeeForm.manager == null){
				  this.employeeForm.manager = {id:null}
        }
				if(row.path != null){
				  let arrays = row.path.split("/");
				  let ids = []
          for(let i=1;i<arrays.length-1;i++){
            ids.push(parseInt(arrays[i]))
          }
          this.employeeForm.parent = ids;
        }
         if(this.employeeForm.parent == null){
           this.employeeForm.parent = {id:null}
         }
        //获取员工数据
        this.gteEmployeeList()
			},
			//显示新增界面
			handleAdd: function () {
				this.employeeFormVisible = true;
        console.log("====================================")
				this.employeeForm = {
          id:null,
          username:"",
          password:"",
          email:"",
          headImage:"",
          age:"",
          department:{
            id:"",
            name:""
          }
        };
        //获取员工数据
        this.gteEmployeeList()
        //获取部门树数据
        this.gteDeptTree()
			},

			//新增修改保存提交
			save: function () {
				this.$refs.employeeForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.employeeFormLoading = true;
							let para = Object.assign({}, this.employeeForm);
              this.$http.put("/employee",para)
                  .then(result =>{
                    result = result.data;
                    if(result.success){
                      this.$message({message: '保存成功', type: 'success'});
                      this.employeeFormLoading = false;
                      this.query.currentPage = 1;
                      this.getEmployees();
                      this.employeeFormVisible = false
                    }else {
                      this.$message({message: '保存失败', type: 'error'});
                    }
                  }).catch(result =>{
                      this.$message({message: '保存失败', type: 'error'});
                   })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEmployees();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getEmployees();
		}
	}

</script>

<style scoped>

</style>